package com.yangrong.thread.chapter6;

/**
 * 打断线程方式结束生命周期
 * 
 * @author YangRong
 * @date 2020年5月26日
 */
public class ThreadCloseGraceful1 {

	private static class Worker extends Thread {
		@Override
		public void run() {
			while (true) {
//				try {
//					Thread.sleep(1);
//				} catch (InterruptedException e) {
//					// 捕获interrupt打断异常，结束线程生命周期，break退出后可以做一些结束操作
//					break;// return
//				}
				if(Thread.interrupted()) {
					break;
				}
			}
			// ...
			System.out.println("结束");
		}
	}

	public static void main(String[] args) {
		Worker worker = new Worker();
		worker.start();

		try {
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		worker.interrupt();
	}
}
